package com.yookey.common.genreate;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.google.common.collect.Lists;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MybatisPlusGenerator {

    //MYSQL //数据库配置四要素
    private static String url = "jdbc:mysql://192.168.1.189:3306/zy_demo?useSSL=false&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=GMT%2B8";
    private static String username = "root";
    private static String password = "admin.yookey";
    private static String authorName = "bjk";
    private static String driverName = "com.mysql.cj.jdbc.Driver";

    //SQLSERVER //数据库配置四要素
    //private static String url = "jdbc:sqlserver://180.97.207.51:11433;DatabaseName=zonghezhifaqzk";
    //private static String username = "sa";
    // private static String password = "admin.yookey";
    //数据库配置四要素
    //private static String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";

    //oracle
//    private static String url ="jdbc:oracle:thin:@localhost:1521/ORCL";
//    private static String username = "ZHZF";
//    private static String password = "123456";
//    private static String driverName = "oracle.jdbc.OracleDriver";
//    private static String authorName = "bjk";

    private static String parentPackageName = "com.yookey";
    private static String entityPackageName = "model.entity";
    private static String mapperPackageName = "mapper";
    private static String servicePackageName = "service";
    private static String serviceImplPackageName = "service.impl";
    private static String controllerPackageName = "controller";


    private static List<String> tableNameList = Lists.newArrayList();


    public static void main(String[] args) {
        tableNameList.add("book");
        tableNameList.stream().forEach(MybatisPlusGenerator::autoGenerator);
    }

    public static void autoGenerator(String tableName) {
        new AutoGenerator()
                .setGlobalConfig(getGlobalConfig())
                .setDataSource(getDataSourceConfig())
                .setPackageInfo(getPackageConfig(parentPackageName))
                .setStrategy(getStrategyConfig(tableName))
                .setCfg(getInjectionConfig())
                .execute();

    }

    private static String getDateTime() {
        LocalDateTime localDate = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return localDate.format(formatter);
    }

    private static InjectionConfig getInjectionConfig() {
        return new InjectionConfig() {
            @Override
            public void initMap() {
                Map map = new HashMap();
                map.put("dateTime", getDateTime());
                setMap(map);
                final String projectPath = System.getProperty("user.dir");
                List<FileOutConfig> fileOutConfigList = new ArrayList<FileOutConfig>();
                // 自定义配置会被优先输出
                fileOutConfigList.add(new FileOutConfig("/templates/mapper.xml.vm") {
                    @Override
                    public String outputFile(TableInfo tableInfo) {
                        // 自定义输出文件名，如果entity设置了前后缀，此次注意xml的名称也会跟着发生变化
                        return projectPath + "/src/main/resources/mapper/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
                    }
                });
                fileOutConfigList.add(new FileOutConfig("/templates/mapper.java.vm") {
                    @Override
                    public String outputFile(TableInfo tableInfo) {
                        return projectPath + "/src/main/java/com/yookey/mapper/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_JAVA;
                    }
                });
                fileOutConfigList.add(new FileOutConfig("/templates/entity.java.vm") {
                    @Override
                    public String outputFile(TableInfo tableInfo) {
                        return projectPath + "/src/main/java/com/yookey/model/entity/" + tableInfo.getEntityName() + StringPool.DOT_JAVA;
                    }
                });
                fileOutConfigList.add(new FileOutConfig("/templates/service.java.vm") {
                    @Override
                    public String outputFile(TableInfo tableInfo) {
                        return projectPath + "/src/main/java/com/yookey/service/I" + tableInfo.getEntityName() + "Service" + StringPool.DOT_JAVA;
                    }
                });
                fileOutConfigList.add(new FileOutConfig("/templates/serviceImpl.java.vm") {
                    @Override
                    public String outputFile(TableInfo tableInfo) {
                        return projectPath + "/src/main/java/com/yookey/service/impl/" + tableInfo.getEntityName() + "ServiceImpl" + StringPool.DOT_JAVA;
                    }
                });
                fileOutConfigList.add(new FileOutConfig("/templates/controller.java.vm") {
                    @Override
                    public String outputFile(TableInfo tableInfo) {
                        return projectPath + "/src/main/java/com/yookey/controller/" + tableInfo.getEntityName() + "Controller" + StringPool.DOT_JAVA;
                    }
                });
                setFileOutConfigList(fileOutConfigList);
            }
        };
    }


    private static StrategyConfig getStrategyConfig(String tableName) {
        return new StrategyConfig()
                .setNaming(NamingStrategy.underline_to_camel)
                .setColumnNaming(NamingStrategy.no_change)
                .setInclude(tableName)
                .setRestControllerStyle(Boolean.TRUE)
                .setEntityLombokModel(Boolean.TRUE)
                .setEntityBuilderModel(true)
                .setControllerMappingHyphenStyle(true)
                .entityTableFieldAnnotationEnable(true);
    }

    private static PackageConfig getPackageConfig(String packageName) {
        return new PackageConfig()
                .setParent(packageName)
                .setService(servicePackageName)
                .setMapper(mapperPackageName)
                .setServiceImpl(serviceImplPackageName)
                .setController(controllerPackageName)
                .setEntity(entityPackageName);
    }

    private static DataSourceConfig getDataSourceConfig() {
        return new DataSourceConfig()
                .setUrl(url)
                .setDriverName(driverName)
                .setUsername(username)
                .setPassword(password);
    }

    private static GlobalConfig getGlobalConfig() {
        return new GlobalConfig()
                .setFileOverride(Boolean.TRUE)
                .setDateType(DateType.ONLY_DATE)
                .setIdType(IdType.AUTO)
                .setAuthor(authorName)
                .setBaseColumnList(true)
                .setSwagger2(true)
                .setEnableCache(false)
                .setBaseResultMap(true)
                .setOpen(false);
    }
}
